package com.example.spinning.controller;

import com.example.spinning.bean.Module;
import com.example.spinning.bean.Operation;
import com.example.spinning.bean.Organ;
import com.example.spinning.bean.RespBean;
import com.example.spinning.service.OrganService;
import com.example.spinning.service.PrivService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限管理
 */
@RestController
@RequestMapping("/priv")
public class PrivController {
    @Autowired
    PrivService privService;

    //模块
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Module> getAllModules() {
        return privService.getAllModules();
    }

    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public RespBean deleteById(@PathVariable String ids) {
        boolean result = privService.deleteModuleByIds(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public RespBean addNewModule(Module module) {
        if ("".equals(module.getModule()) || module.getModule() == null) {
            return new RespBean("error", "请输入模块名!");
        }
        int result = privService.addModule(module);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateModule(Module module) {
        int i = privService.updateModuleById(module);
        if (i == 1) {
            return new RespBean("success", "修改成功!");
        }
        return new RespBean("error", "修改失败!");
    }

    //操作
    @RequestMapping(value = "/oper/all", method = RequestMethod.GET)
    public List<Operation> getAllOperations() {
        return privService.getAllOperations();
    }

    @RequestMapping(value = "/oper/{ids}", method = RequestMethod.DELETE)
    public RespBean deleteOperatonById(@PathVariable String ids) {
        boolean result = privService.deleteOperationByIds(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    @RequestMapping(value = "/oper/", method = RequestMethod.POST)
    public RespBean addNewOperation(Operation operation) {
        if ("".equals(operation.getOperation()) || operation.getOperation() == null) {
            return new RespBean("error", "请输入模块名!");
        }
        int result = privService.addOperation(operation);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    @RequestMapping(value = "/oper/", method = RequestMethod.PUT)
    public RespBean updateOperation(Operation operation) {
        int i = privService.updateOperationById(operation);
        if (i == 1) {
            return new RespBean("success", "修改成功!");
        }
        return new RespBean("error", "修改失败!");
    }
    //模块+操作
//    添加权限
    @RequestMapping(value = "/priv/", method = RequestMethod.POST)
    public RespBean addPriv(String privilege) {
        int i = privService.addPriv(privilege);
        if (i == 1) {
            return new RespBean("success", "添加成功!");
        }
        if(i==2){
            return new RespBean("error","该权限已存在");
        }
        return new RespBean("error", "添加失败!");
    }
//    修改权限
    @RequestMapping(value = "/priv/", method = RequestMethod.PUT)
    public RespBean updatePriv(Module module) {
        int i = privService.updatePriv(module);
        if (i == 1) {
            return new RespBean("success", "修改成功!");
        }
        if(i==2){
            return new RespBean("error","修改后的权限已存在");
        }
        return new RespBean("error", "修改失败!");
    }
    @RequestMapping(value = "/priv/all", method = RequestMethod.GET)
    public Map<String,Object> getAllPrivs(@RequestParam(value = "page") Integer page,
                                          @RequestParam(value = "count") Integer count,
                                          Module module) {
        int totalCount = privService.getPrivCountByCondition(module);
        List<Module> priv = privService.getPrivByCondition(page,count,module);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("priv",priv);
        return map;

    }

//    删除权限
    @RequestMapping(value = "/priv/{ids}", method = RequestMethod.DELETE)
    public RespBean deletePrivs(@PathVariable String ids) {
        boolean result = privService.deletePrivs(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }
//查询权限不带分页
    @RequestMapping(value = "/priv/getPrivs", method = RequestMethod.GET)
    public Map<String,Object> getPrivs(Module module) {

        List<Module> priv = privService.getPrivs(module);
        Map<String,Object> map = new HashMap<>();
        map.put("priv",priv);
        return map;

    }







}
